/*
 * c7_exercise2.cpp
 *
 *  Created on: Sep 2, 2013
 *      Author: cyper.yin
 */

#include <iostream>
#include <array>
using namespace std;

int input_score(double scores[], int size);
void display_score(const double scores[], int size);
double calc_avg(const double scores[], int size);

int main_c7e2(int argc, char **argv) {
	const int SIZE = 10;
	double scores[SIZE];

	int actual = input_score(scores, SIZE);
	display_score(scores, actual);

	cout <<"average: " << calc_avg(scores, actual) << endl;
	return 0;
}

int input_score(double scores[], int size) {
	cout << "Enter up to 10 golf scores, press q to quit:";

	int i = 0;
	while (cin >> scores[i] && i++ < size) {
		cout << "Enter up to 10 golf scores, press q to quit:";
	}

	if (!cin) {
		cin.clear();
		while (cin.get() != '\n') {
			continue;
		}
	}

	return i;
}

void display_score(const double scores[], int size) {
	for (int i = 0; i < size; i++) {
		cout << scores[i] << " ";
	}

	cout << endl;
}

double calc_avg(const double scores[], int size) {
	double sum = 0;
	for (int i = 0; i < size; i++) {
		sum += scores[i];
	}
	return sum / size;
}
